package com.briup.shopadmin.service.impl;

import com.briup.shopadmin.common.GlobalException;
import com.briup.shopadmin.common.bean.Admin;
import com.briup.shopadmin.common.utils.JwtUtil;
import com.briup.shopadmin.dao.IAdminDao;
import com.briup.shopadmin.service.IAdminservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class AdminServiceImpl implements IAdminservice {
    @Autowired
    IAdminDao adminDao;
    @Override
    public Map<String, String> login(String username,String password) {
        HashMap<String,String> map = new HashMap<>();
        Admin admin = adminDao.findByUsername(username);
        if (admin == null) {
            throw new GlobalException(5004, "用户不存在");
        }else {
            if (admin.isStats()){

                if (password.equals(admin.getPassword())){
                    String token = JwtUtil.createJWT(username);
                    map.put("token",token);
                    map.put("username",username);
                    return map;

                }else {
                    throw new GlobalException(5006,"用户名密码错误");
                }
            }else {
                throw new GlobalException(5005,"账号不可用，联系系统管理员");
            }
        }
    }
}
